package cn.xiaopengstack.domain.activity.service.quota;

import cn.xiaopengstack.domain.activity.model.aggregate.CreateRechangeQuotaOrderAggregate;
import cn.xiaopengstack.domain.activity.model.entity.*;
import cn.xiaopengstack.domain.activity.model.valobj.ActivitySkuStockKeyVO;
import cn.xiaopengstack.domain.activity.repository.IRaffleActivityRepository;
import cn.xiaopengstack.domain.activity.service.IRaffleActivitySkuStockService;
import cn.xiaopengstack.domain.activity.service.quota.policy.IRechargePolicy;
import cn.xiaopengstack.domain.activity.service.quota.rule.factory.DefaultActivityActionChainFactory;
import cn.xiaopengstack.types.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * @author jiangyangang
 */
@Component
@Slf4j
public class RaffleActivityQuotaService extends AbsRaffleActivityQuotaService implements IRaffleActivitySkuStockService {

    public RaffleActivityQuotaService(Map<String, IRechargePolicy> tradePolicyGroup, DefaultActivityActionChainFactory defaultActivityActionChainFactory, IRaffleActivityRepository activityRepository) {
        super(tradePolicyGroup, defaultActivityActionChainFactory, activityRepository);
    }

    @Override
    protected CreateRechangeQuotaOrderAggregate buildOrderAggregate(ActivitySkuRechargeEntity skuRechargeEntity, RaffleActivitySkuEntity activitySkuEntity, RaffleActivityEntity activityEntity, RaffleActivityCountEntity activityCountEntity) {
        // 订单实体对象
        RaffleActivityOrderEntity activityOrderEntity = new RaffleActivityOrderEntity();
        activityOrderEntity.setUserId(skuRechargeEntity.getUser().getUserId());
        activityOrderEntity.setSku(skuRechargeEntity.getSku());
        activityOrderEntity.setActivityId(activityEntity.getActivityId());
        activityOrderEntity.setActivityName(activityEntity.getActivityName());
        activityOrderEntity.setStrategyId(activityEntity.getStrategyId());
        // 公司里一般会有专门的雪花算法UUID服务，我们这里直接生成个12位就可以了。
        activityOrderEntity.setOrderId(RandomStringUtils.randomNumeric(12));
        activityOrderEntity.setOrderTime(new Date());
        activityOrderEntity.setTotalCount(activityCountEntity.getTotalCount());
        activityOrderEntity.setDayCount(activityCountEntity.getDayCount());
        activityOrderEntity.setMonthCount(activityCountEntity.getMonthCount());
        activityOrderEntity.setOutBusinessNo(skuRechargeEntity.getOutBusinessNo());

        // 构建聚合对象
        return CreateRechangeQuotaOrderAggregate.builder()
                .userId(skuRechargeEntity.getUser().getUserId())
                .activityEntity(activityEntity)
                .totalCount(activityCountEntity.getTotalCount())
                .dayCount(activityCountEntity.getDayCount())
                .monthCount(activityCountEntity.getMonthCount())
                .raffleActivityOrderEntity(activityOrderEntity)
                .rechargeTime(skuRechargeEntity.getRechargeTime())
                .skuEntity(activitySkuEntity)
                .build();
    }

    @Override
    public ActivitySkuStockKeyVO consumeActivitySkuStockDecrQueue() throws InterruptedException {
        return activityRepository.consumeActivitySkuStockDecrQueue();
    }

    @Override
    public void clearActivitySkuStockDecrQueue(ActivitySkuStockKeyVO stockKeyVO) {
        activityRepository.clearActivitySkuStockDecrQueue(stockKeyVO);
    }

    @Override
    public void updateActivitySkuStock(Long sku, Integer decrNumber) {
        activityRepository.updateActivitySkuStock(sku, decrNumber);
    }

    @Override
    public void clearActivitySkuStockQueue(Long sku) {
        activityRepository.clearActivitySkuStock(sku);
    }

    @Override
    public Integer queryRaffleActivityAccountDayPartakeCount(Long activityId, String userId) {
//        return activityRepository.(activityId, userId);
        return 0;
    }

    @Override
    public RaffleActivityAccountEntity queryActivityAccountPartakeAccount(Long activityId, String userId) {

        // 总账户
        RaffleActivityAccountEntity accountEntity = activityRepository.queryActivityAccount(activityId, userId);
        if (Objects.isNull(accountEntity)) {
            return RaffleActivityAccountEntity.initZeroAccount(activityId, userId);
        }

        // 日账户
        ActivityAccountDayEntity activityAccountDayEntity = activityRepository.queryActivityAccountDayByUserId(userId, activityId, TimeUtils.dateToString(new Date(), TimeUtils.YYYY_MM_DD));
        if (Objects.nonNull(activityAccountDayEntity)) {
            accountEntity.setDayCount(activityAccountDayEntity.getDayCount());
            accountEntity.setDayCountSurplus(activityAccountDayEntity.getDayCountSurplus());
        }

        // 月账户
        ActivityAccountMonthEntity activityAccountMonthEntity = activityRepository.queryActivityAccountMonthByUserId(userId, activityId, TimeUtils.dateToString(new Date(), TimeUtils.YYYY_MM));
        if (Objects.nonNull(activityAccountMonthEntity)) {
            accountEntity.setMonthCount(activityAccountMonthEntity.getMonthCount());
            accountEntity.setMonthCountSurplus(activityAccountMonthEntity.getMonthCountSurplus());
        }

        return accountEntity;
    }

    @Override
    public Integer queryActivityAccountPartakeCount(Long activityId, String userId) {
        RaffleActivityAccountEntity accountEntity = activityRepository.queryActivityAccount(activityId, userId);
        if (Objects.isNull(accountEntity)) {
            return 0;
        }
        return accountEntity.getTotalCount() - accountEntity.getTotalCountSurplus();
    }
}
